package br.com.cadastro.web.componentes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;

import br.com.cadastro.entidade.Disciplina;
import br.com.cadastro.persistencia.dao.DisciplinaDAO;

public class SortableDisciplinaDataProvider extends SortableDataProvider<Disciplina> {

	private static final long serialVersionUID = -6684443220048744254L;

	
	private List<Disciplina> list = new ArrayList<Disciplina>();
	private DisciplinaDAO disciplinaDAO;
	
	public SortableDisciplinaDataProvider(){
		setSort("categoria",true);
		disciplinaDAO = new DisciplinaDAO();
		list = disciplinaDAO.listarTodos();
	}
	
	public Iterator<Disciplina> iterator(int first, int count) {
		// In this example the whole list gets copied, sorted and sliced; in real applications typically your database would deliver a sorted and limited list 

		SortParam sp = getSort();
		List<Disciplina> newList  = disciplinaDAO.listarPorColuna(sp.getProperty(),sp.isAscending());
		

		return newList.subList(first, first + count).iterator();
	}

	public IModel<Disciplina> model(final Disciplina object) {
		return new AbstractReadOnlyModel<Disciplina>() {
			
			private static final long serialVersionUID = 1L;

			@Override
			public Disciplina getObject() {
				return (Disciplina) object;
			}
		};
	}

	public int size() {
		return list.size();
	}

}
